package light.head.controller.site;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.site.SettledApplyDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.Organ;
import light.head.model.OrganDetails;
import light.head.model.SettledApply;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(Url.SETTLEDAPPLY)
public class SettledApplyController extends SupporterController{
	private SettledApplyDAO saDAO;
	
	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.SETTLEDAPPLY_INIT);
	}
	
	@RequestMapping(Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.STATUS);
		if (saDAO.findByMap(map, pb) == RESULT.SUCCESS) {
			request.setAttribute(Parm.PAGE_BEAN, pb);
		}
		return new ModelAndView(Url.SETTLEDAPPLY_LIST);
	}


	@RequestMapping(value = Url.APPLY)
	public void save(SettledApply sa, HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		if(sa.getCode() == null){
			sa.setCt(new Date());
			sa.setDeleted(false);
			sa.setStatus(false);
			result = saDAO.add(sa);
		}
		if(result == RESULT.SUCCESS){
			print(response,Msg.SUCCESS);
		}else{
			print(response,Msg.ERROR);
		}
	}
	@RequestMapping(value = Url.AID_DETAIL)
	public ModelAndView aidDetail(Integer code, HttpServletRequest request, HttpServletResponse response) throws Exception {
		SettledApply info = saDAO.get(code);
		if(info != null ){
			request.setAttribute(Parm.INFO, info);
		}
		if(info.getStatus() == false){
			info.setStatus(true);
			saDAO.upd(info);
		}
		return new 	ModelAndView(Url.SETTLEDAPPLY_DETAIL);
	}
	
	@RequestMapping(value = Url.DELETE)
	public void delete(Integer code, HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		SettledApply sa = saDAO.get(code);
		if(sa != null ){
			sa.setDeleted(true);
			result = saDAO.upd(sa);
		}
		if (result == RESULT.SUCCESS) {
			print(response, Msg.SUCCESS);
		} else {
			print(response, Msg.ERROR);
		}
	}
	
	@RequestMapping(value = Url.EDIT)
	public ModelAndView edit(Integer code, HttpServletRequest request, HttpServletResponse response) throws Exception {
		SettledApply sa = saDAO.get(code);
		if(sa != null){
			Organ info = new Organ();
			info.setName(sa.getName());
			info.setManager(sa.getContact());
			info.setDetails(new OrganDetails());
			info.getDetails().setEmail(sa.getEmail());
			info.getDetails().setPhone(sa.getPhone());
			request.setAttribute(Parm.ORGAN, info);
		}
		return new ModelAndView(Url.ORGAN_EDIT);
	}
	@Autowired
	public void setSaDAO(SettledApplyDAO saDAO) {
		this.saDAO = saDAO;
	}
	
}
